<?php
$include_path = "../";
include_once("../util/db_manager.php");
require_once("admin_data_base_functions.php");
$error = false;
$msg = "";
$total = 0;
class Artifact {
	var $artifact_id;
	var $artifact_type;
	var $title;
	var $description;
	var $url;
	var $last_update_time;
	var $visitor_count;
	var $title_img_url;
	var $plant_id;
	
	public static function getArtifactList($result) {
		$artifacts = array();
		while ($row = mysql_fetch_array($result)) {
			$artifact = new Artifact();
			$artifact->artifact_id = $row["artifact_id"];
			$artifact->artifact_type = $row["artifact_type"];
			$artifact->last_update_time = $row["dt"];
			$artifact->url = $row["url"];
			$artifact->title = $row["title"];
			$artifact->description = $row["description"];
			$artifact->visitor_count = $row["visitor_count"];
			$artifact->title_img_url = $row["title_img_url"];
			$artifact->plant_id = $row["plant_id"];
			
			$artifacts[] = $artifact;
		}
		return $artifacts;
	}
	
	public static function publishArtifact($artifact_id) {
		$url = "";
		try {
			//start db transaction
			$dbManager = DBManager::getInstance($GLOBALS['config_file']);
			$conn = $dbManager->fetchConnection();
			
			$query = "UPDATE artifacts SET published=1 WHERE artifact_id=" . $artifact_id;
			$dbManager->processQuery($query,$conn);
			$query = "SELECT url FROM artifacts WHERE artifact_id=" . $artifact_id;
			$result = $dbManager->processQuery($query,$conn);
			while ($row = mysql_fetch_array($result)) {
				$url = $row["url"];
			}
			$dbManager->releaseConnection($conn);
			$GLOBALS['msg'] = "completed";
		} catch (Exception $e) {
			$GLOBALS['error'] = true;
			$GLOBALS['msg'] = $e->getMessage();
		} 
			return $url;
	}
	
	public static function updateArtifact($artifact_id, $artifact_type, $title, $desc, $url, $title_img_url, $plant_id) {
		try {
			//start db transaction
			$dbManager = DBManager::getInstance($GLOBALS['config_file']);
			$conn = $dbManager->fetchConnection();
			
			$query = "UPDATE artifacts SET published=0, title='" . $title . "', description='" . $desc . "'";
			if ($artifact_type == "P") $query = $query . ", plant_id=" . $plant_id;
			if (isset($url)) $query = $query . ", plant_id=" . $plant_id;
			if (isset($title_img_url)) $query = $query . ", title_img_url=" . $title_img_url;
			
			$query = $query . " WHERE artifact_id=" . $artifact_id;
			$dbManager->processQuery($query,$conn);
			$dbManager->releaseConnection($conn);
			$GLOBALS['msg'] = "completed";
		} catch (Exception $e) {
			$GLOBALS['error'] = true;
			$GLOBALS['msg'] = $e->getMessage();
		} 
			$response = getResponseStart($GLOBALS['error'], $GLOBALS['msg'], $GLOBALS['total']);
			$response = $response . getResponseEnd();
			return $response;
	}
	
	public static function addArtifact($type, $title, $desc, $url, $title_img_url, $plant_id) {
		try {
			//start db transaction
			$dbManager = DBManager::getInstance($GLOBALS['config_file']);
			$conn = $dbManager->fetchConnection();
			
			$query = "INSERT INTO artifacts(title, description, url, title_img_url, artifact_type, plant_id) VALUES('" . $title . "','" . $desc . "','" . $url . "','" . $title_img_url . "','" . $type . "'";
			if ($type == "P") $query = $query . "," . $plant_id . ")";
			else $query = $query . ",null)";
			$dbManager->processQuery($query,$conn);
			$artifact_id = mysql_insert_id();
			$dbManager->releaseConnection($conn);
			$GLOBALS['msg'] = "completed";
		} catch (Exception $e) {
			$GLOBALS['error'] = true;
			$GLOBALS['msg'] = $e->getMessage();
		} 
		return $artifact_id;
	}
	
	public static function getArtifact($artifact_id) {
		try {
			//start db transaction
			$dbManager = DBManager::getInstance($GLOBALS['config_file']);
			$conn = $dbManager->fetchConnection();
			
			$query = "SELECT artifact_id, title, description, plant_id FROM artifacts WHERE artifact_id=" . $artifact_id;
			$result = $dbManager->processQuery($query,$conn);
			$artifacts = Artifact::getArtifactList($result);
			$dbManager->releaseConnection($conn);
			$GLOBALS['total'] = 1;
			$GLOBALS['msg'] = "completed";
		} catch (Exception $e) {
			$GLOBALS['error'] = true;
			$GLOBALS['msg'] = $e->getMessage();
		} 
		return Artifact::getArtifactResponse($artifacts);
	}
	
	public static function getArtifacts($artifact_type) {
		try {
			//start db transaction
			$dbManager = DBManager::getInstance($GLOBALS['config_file']);
			$conn = $dbManager->fetchConnection();
			
			$query = "SELECT artifact_id, title FROM artifacts WHERE artifact_type='" . $artifact_type . "' ORDER BY title";
			$result = $dbManager->processQuery($query,$conn);
			$artifacts = Artifact::getArtifactList($result);
			$dbManager->releaseConnection($conn);
			$GLOBALS['msg'] = "completed";
		} catch (Exception $e) {
			$GLOBALS['error'] = true;
			$GLOBALS['msg'] = $e->getMessage();
		} 
		return Artifact::getArtifactResponse($artifacts);
	}

	public static function getArtifactResponse($artifacts) {
		$response = getResponseStart($GLOBALS['error'], $GLOBALS['msg'], $GLOBALS['total']);
		$response = $response . "<artifacts>";
		if ($artifacts != null) {
			for ($cntr = 0; $cntr < count($artifacts); $cntr++) {
				$artifact = $artifacts[$cntr];
				$response = $response . "<artifact><id>";
				$response = $response . $artifact->artifact_id;
				$response = $response . "</id><type>";
				$response = $response . $artifact->artifact_type;
				$response = $response . "</type><title>";
				$response = $response . "<![CDATA[" . $artifact->title . "]]>";
				$response = $response . "</title><description>";
				$response = $response . "<![CDATA[" . $artifact->description . "]]>";
				$response = $response . "</description><url>";
				$response = $response . "<![CDATA[" . $artifact->url . "]]>";
				$response = $response . "</url><updt_ts>";
				$response = $response . $artifact->last_update_time;
				$response = $response . "</updt_ts><visitors>";
				$response = $response . $artifact->visitor_count;
				$response = $response . "</visitors><title_img>";
				$response = $response . "<![CDATA[" . $artifact->title_img_url . "]]>";
				$response = $response . "</title_img><plant_id>";
				$response = $response . $artifact->plant_id;
				$response = $response . "</plant_id></artifact>";
			}
		}
		$response = $response . "</artifacts>";
		$response = $response . getResponseEnd();
		return $response;
	}
}
?>
